class GridBase extends eui.Component {
	/**占用格子横向数量 */
	public gridH: number = 1;
	/**占用格子竖向数量 */
	public gridV: number = 1;

	public offsetH: number = 0;
	public offsetV: number = 0;

	public block: boolean = true;
	/**格子索引 */
	public index: number;
	public img: eui.Image;
	public constructor() {
		super();
		this.skinName = "resource/tankSkins/Grid.exml";
	}
	clear() {
		this.parent && this.parent.removeChild(this);
	}
	isBlock() {
		return this.block;
	}
	getIndex() {
		return GridBase.pos2index(this.x, this.y);
	}
	/**获取自己占用的格子索引 */
	getGrid() {
		let index = this.getIndex(); //取出坦克左上角的格子。坦克占用4个格子。
		let grid = [];
		for (var j = 0; j < this.gridV; j++) {
			for (var i = 0; i < this.gridV; i++) {
				grid.push(index + j * 40 + this.offsetV * 40 + i + this.offsetH)
			}
		}
		return grid;
	}

	/**获取占用格子的方向对应的格子*/
	getSideGrid(dir: TankDir, offset = 0) {
		let myGrid = this.getGrid();
		let startIndex = {
			[TankDir.Up]: 0,
			[TankDir.Right]: this.gridH - 1,
			[TankDir.Down]: this.gridH * (this.gridV - 1),
			[TankDir.Left]: 0,
		}[dir];
		let lst = [];
		if (dir == TankDir.Up || dir == TankDir.Down) {
			for (var i = 0; i < this.gridH; i++) lst.push(myGrid[startIndex + i]);
			if (offset) lst = lst.map(v => v + (dir == TankDir.Up ? -offset : offset) * 40)
		} else {
			for (var i = 0; i < this.gridV; i++) lst.push(myGrid[startIndex + i * this.gridH]);
			if (offset) lst = lst.map(v => v + (dir == TankDir.Left ? -offset : offset))
		}
		return lst;

	}
	static index2pos(index: number) {
		let {h, v} = this.index2hv(index);
		return { x: gridValue * h, y: v * gridValue };
	}
	static index2hv(index: number) {
		return {
			h: index % 40,
			v: (index / 40 | 0)
		};
	}
	static pos2index(x: number, y: number) {
		let h = x / gridValue | 0;
		let p = y / gridValue | 0;
		return h + p * 40;
	}
	static hv2index(h: number, v: number) {
		return v * 40 + h;
	}
	static contains(girdLst: number[], girdLst2: number[]) {
		for (let grid of girdLst) {
			if (girdLst2.indexOf(grid) >= 0) {
				return true;
			}
		}
	}
	playEff(aniName: string, func: Function, times = 1) {
		let eff = Pool.getItem(TankEff);
		eff.gotoAndPlay(aniName, times);
		this.addChild(eff);
		let cmp = e => {
			func && func();
			eff.removeEventListener(egret.Event.COMPLETE, cmp, this);
			eff.clear();
		}
		eff.addEventListener(egret.Event.COMPLETE, cmp, this);
	}
	$onAddToStage(stage: egret.Stage, nestLevel: number) {
		this.block = true;
		super.$onAddToStage(stage, nestLevel);
	}
	$onRemoveFromStage() {
		this.block = false;
		Pool.recover(this);
		super.$onRemoveFromStage();
	}

}